package com.itheima.utils;

import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class JSONFileUtils {

    public static String readFile(String filepath) throws Exception {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filepath);
            return IOUtils.toString(fis, StandardCharsets.UTF_8);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

    public static void writeFile(String data, String filepath) throws Exception {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filepath);
            IOUtils.write(data, fos, StandardCharsets.UTF_8);
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}